﻿using System;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
using ConfigLibrary;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;

namespace Contacts.Mail
{
    /// <summary>
    /// Use to send email to a specific address.
    /// The default is set in the global Project settings
    /// </summary>
    public static class mailman
    {

        static bool mailSent = false;
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            String token = (string)e.UserState;

            if (e.Cancelled)
            {
                //Console.WriteLine("[{0}] Send canceled.", token);
            }
            if (e.Error != null)
            {
                // Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
                // System.Windows.Forms.MessageBox.Show(e.Error.ToString());
            }
            else
            {
                //Console.WriteLine("Message sent.");
                //System.Windows.Forms.MessageBox.Show("email sent");
            }
            mailSent = true;
        }


        /// <summary>
        /// Call the function with the subject and message you want to send
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="msg"></param>
        public static void sendmail(string subject, string msg)
        {
#if RELEASE 
            //loads the config file
            Config config;
            Stream s = File.Open(Properties.Settings.Default.CONFIGPATH, FileMode.Open);
            BinaryFormatter b = new BinaryFormatter();
            config = (Config)b.Deserialize(s);
            s.Close();
            //end load

            //make client
            SmtpClient client = new SmtpClient(config.MailHost, config.MailPort);
            // set credentials
            client.UseDefaultCredentials = true;

            // Specify the e-mail sender.
            // Create a mailing address that includes a UTF8 character
            // in the display name.
            MailAddress from = new MailAddress("RHA@rhaaia.com", "RHA Email", System.Text.Encoding.UTF8);
            // Set destinations for the e-mail message.
            MailMessage message;
            //if (address.Count > 0)
            //{
            //    MailAddress to = new MailAddress("RHA@rhaaia.com");
            //    message = new MailMessage(from, to);
            //    foreach (string email in address)
            //    {
            //        MailAddress t = new MailAddress(email);
            //        message.To.Add(t);
            //    }
            //}
            //else
            //{
            //    MailAddress to = new MailAddress(address[0]);
            //    message = new MailMessage(from, to);
            //}

            MailAddress to = new MailAddress("dplummer@rhaaia.com");
            //creat the message
            message = new MailMessage(from, to);
            // add on extra recipients and carbon copies
            MailAddress to2 = new MailAddress("bcrawford@rhaaia.com");
            //MailAddress cc1 = new MailAddress("dplummer@rhaaia.com");
            //MailAddress cc2 = new MailAddress("bcrawford@rhaaia.com");
            message.To.Add(to2);
            //message.CC.Add(cc1);
            //message.CC.Add(cc2);

            
            
            // Specify the message content.e + 
            message.Body += "\nMessage from Contacts\n\n";
            message.Body += string.Format("User: {0}\n\nOS: {1}\n\nProc count: {2}\n\nPhysical mapped memory: {3} Kb\n\nMessage: {4}\n",
                Environment.UserName, Environment.OSVersion, Environment.ProcessorCount, Environment.WorkingSet/1000, msg);
            
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Subject = subject;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            // Set the method that is called back when the send operation ends.
            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
            // The userState can be any object that allows your callback 
            // method to identify this send operation.
            // For this example, the userToken is a string constant.
            string userState = "message";
            client.SendAsync(message, userState);
            //Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
            //string answer = Console.ReadLine();
            // If the user canceled the send, and mail hasn't been sent yet,
            // then cancel the pending operation.
            //if (answer.StartsWith("c") && mailSent == false)
            //{
            //    client.SendAsyncCancel();
            //}
            // Clean up.
            // message.Dispose();
#endif
        }
    }
}
